<?php

namespace App\Http\Controllers\Admin\Money;

use App\Http\Controllers\Admin\BaseAdminController;
use App\Models\Moneys;
use App\Models\Users;

/**
 * 订单管理控制器
 *
 * @author yh
 * @package App\Http\Controllers\Orders
 */
class MoneysController extends BaseAdminController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 商家列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20200318
     */
    public function index()
    {
        // 处理排序
        $sort = request('sort', 'id');
        $order = request('order', 'desc');

        $query = Moneys::orderBy($sort, $order)->where('status','!=',1);
            if (request('title')) {
                $query->where('username', 'LIKE', "%" . request('username') . "%");
            }
            if(request('phone')){
                $query->where('phone','LIKE', "%" . request('phone') . "%");
            }
            if(request('card_num')){
                $query->where('card_num','LIKE', "%" . request('card_num') . "%");
            }
            // 取得列表
            $shops = $query->select([
                'id',
                'username',
                'phone',
                'card_num',
                'card_type',
                'card_site',
                'money',
                'num',
                'openid',
                'status',
                'created_at',
                'refund_msg'
            ])->paginate(parent::PERPAGE);
            // 将查询参数拼接到分页链接中
            $shops->appends(app('request')->all());

            return view('admin.money.moneys', [
                'orders' => $shops,
            ]);
        }

        /**
         * 审核列表
         *
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         * @author yh 20200318
         */
        public
        function money_shen()
        {
            // 处理排序
            $sort = request('sort', 'id');
            $order = request('order', 'desc');
    
            $query = Moneys::orderBy($sort, $order)->where('status',1);
                if (request('title')) {
                    $query->where('username', 'LIKE', "%" . request('username') . "%");
                }
                if(request('phone')){
                    $query->where('phone','LIKE', "%" . request('phone') . "%");
                }
                if(request('card_num')){
                    $query->where('card_num','LIKE', "%" . request('card_num') . "%");
                }
                // 取得列表
                $shops = $query->select([
                    'id',
                    'username',
                    'phone',
                    'card_num',
                    'card_type',
                    'card_site',
                    'money',
                    'status',
                    'openid',
                    'num',
                    'created_at',
                    'refund_msg'
                ])->paginate(parent::PERPAGE);
                // 将查询参数拼接到分页链接中
                $shops->appends(app('request')->all());
    
                return view('admin.money.moneys_shen', [
                    'orders' => $shops,
                ]);
            }
        /**
         * 确认完成兑换商品订单
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         * @author yh 20200318
         */
        public  function money_refund(){
            $id = request('id');
            $order = Moneys::where('id', $id)->first();
            $order->refund_msg = request('refund_msg');
            $order->status = 3;
            $order->save();
            $user=User::where('openid',$order->openid)->first();
            $user->money =$user->money+$order->money;
            $user->save();
            return $this->success();
        }
    /**
     * 完成提现申请
     *
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     * @author lxp 20160713
     */
    public function finish($id)
    {
        if (request()->ajax() && intval($id)) {
            $order = Moneys::where('id', $id)->first();
            $order->status = 2;
            $order->save();
            return $this->success();
        }
    }
    }